package org.afs.gescol.gwt.client.view;

import org.afs.gescol.gwt.client.bundle.GescolResources;
import org.afs.gescol.gwt.client.factory.AppClientFactory;
import org.afs.gescol.gwt.client.i18n.AppMessages;
import org.afs.gescol.gwt.client.presenter.HomePresenter.HomeView;
import org.afs.gescol.gwt.client.util.ActionsKeysUtil;
import org.afs.gescol.gwt.client.util.SortKeysUtil;
import org.afs.gescol.gwt.shared.CellPersonneModel;
import org.scub.foundation.framework.gwt.module.client.util.composants.pagination.Pagination.Display;
import org.scub.foundation.framework.gwt.module.client.util.composants.remote.paging.RemotePagingTable;
import org.scub.foundation.framework.gwt.module.client.util.composants.remote.paging.cell.ButtonDefinition;
import org.scub.foundation.framework.gwt.module.client.util.composants.remote.paging.cell.ButtonGroupCell;
import org.scub.foundation.framework.gwt.module.client.util.composants.remote.paging.event.HasRemotePagingTableHandlers;
import org.scub.foundation.framework.gwt.module.client.util.composants.suggestbox.SuggestListBoxSingle;
import org.scub.foundation.framework.gwt.module.client.util.composants.suggestbox.SuggestListBoxSingle.SuggestListBoxSingleProperties;
import org.scub.foundation.framework.gwt.module.shared.IdLabelModel;

import com.github.gwtbootstrap.client.ui.Button;
import com.github.gwtbootstrap.client.ui.TextBox;
import com.github.gwtbootstrap.client.ui.constants.IconType;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.HasKeyUpHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;

/**
 * Implementation of home view.
 * @autor Kobla GBENYO (kobla.gbenyo@gmail.com)
 */
public class HomeViewImpl extends Composite implements HomeView {

    private Binder binder = AppClientFactory.getBinder();

    private AppMessages messages = AppClientFactory.getMessages();

    private GescolResources ressources = AppClientFactory.getResources();

    private SortKeysUtil sortUtils = AppClientFactory.getSortKeysUtil();

    private ActionsKeysUtil actionsUtils = AppClientFactory.getActionsKeysUtil();

    /** Search loader. */
    @UiField(provided = true)
    Image loader;

    /** The add personnel's button. */
    @UiField
    Button addPersonnelBtn;

    /** The add eleve's button. */
    @UiField
    Button addEleveBtn;

    /** The field to filter results. */
    @UiField
    TextBox tbSearch;

    /** The field to specify search type. */
    @UiField(provided = true)
    SuggestListBoxSingle<IdLabelModel> slbTypeModelSearch;

    /** The contact's Grid. */
    @UiField(provided = true)
    RemotePagingTable<CellPersonneModel> remotePagingTable;

    private Column<CellPersonneModel, String> buttonsColumns;

    /**
     * An interface required by the UiBinder.
     */
    public interface Binder extends UiBinder<Widget, HomeViewImpl> {
    }

    @Override
    public void init() {
        slbTypeModelSearch = new SuggestListBoxSingle<IdLabelModel>(new SuggestListBoxSingleProperties<IdLabelModel>() {
            @Override
            public String getSelectSuggestRenderer(IdLabelModel row) {
                return row != null && row.getLabel() != null ? row.getLabel() : "";
            }
            @Override
            public String[] getResultColumnsRenderer() {
                return new String[] {messages.lblLibelle()};
            }
            @Override
            public String[] getResultRowsRenderer(IdLabelModel row) {
                return new String[] {row != null && row.getLabel() != null ? row.getLabel() : ""};
            }
        });
        slbTypeModelSearch.setWidth(AppMessages.PIXELS_70);
        initCellTable();
        initLoader();
        initWidget(binder.createAndBindUi(this));
        addPersonnelBtn.ensureDebugId("debugIdBtnAddPersonnel");
    }

    /**
     * Init the loader picture.
     */
    private void initLoader() {
        loader = new Image(ressources.loader());
        loader.getElement().getStyle().setMargin(5, Unit.PX);
        loader.setVisible(false);
    }

    private void initCellTable() {
        remotePagingTable = new RemotePagingTable<CellPersonneModel>(AppMessages.REMOTE_PAGING_TABLE_PAGE_SIZE, Display.AROUND);

        final TextColumn<CellPersonneModel> matriculeColumn = new TextColumn<CellPersonneModel>() {
            @Override
            public String getValue(CellPersonneModel personnel) {
                return personnel.getNumber();
            }
        };
        final TextColumn<CellPersonneModel> civilityColumn = new TextColumn<CellPersonneModel>() {
            @Override
            public String getValue(CellPersonneModel personnel) {
                return personnel.getCivility();
            }
        };
        final TextColumn<CellPersonneModel> lastNameColumn = new TextColumn<CellPersonneModel>() {
            @Override
            public String getValue(CellPersonneModel personnel) {
                return personnel.getLastName();
            }
        };
        final TextColumn<CellPersonneModel> firstNameColumn = new TextColumn<CellPersonneModel>() {
            @Override
            public String getValue(CellPersonneModel personnel) {
                return personnel.getFirstName();
            }
        };
        final ButtonDefinition edit = new ButtonDefinition(new Button("", IconType.EDIT), actionsUtils.edit());
        final ButtonDefinition del = new ButtonDefinition(new Button("", IconType.REMOVE), actionsUtils.delete());
        buttonsColumns = new Column<CellPersonneModel, String>(new ButtonGroupCell(edit, del)) {
            @Override
            public String getValue(CellPersonneModel object) {
                return "";
            }
        };
        remotePagingTable.addColumn(matriculeColumn, messages.matricule(), sortUtils.matricule(), 15, Unit.PCT);
        remotePagingTable.addColumn(civilityColumn, messages.civility(), sortUtils.civility(), 10, Unit.PCT);
        remotePagingTable.addColumn(lastNameColumn, messages.lastName(), sortUtils.lastName(), 27, Unit.PCT);
        remotePagingTable.addColumn(firstNameColumn, messages.firstName(), sortUtils.firstName(), 40, Unit.PCT);
        remotePagingTable.addColumn(buttonsColumns, 8, Unit.PCT);
        remotePagingTable.showSort(lastNameColumn);
    }

    @Override
    public Widget asWidget() {
        return this;
    }

    @Override
    public HasClickHandlers getAddPersonnelBtn() {
        return addPersonnelBtn;
    }

    @Override
    public HasClickHandlers getAddEleveBtn() {
        return addEleveBtn;
    }

    @Override
    public Widget getPrincipalContainer() {
        return getWidget();
    }

    @Override
    public HasValue<String> getSearch() {
        return tbSearch;
    }

    @Override
    public HasKeyUpHandlers getSearchHandler() {
        return tbSearch;
    }

    @Override
    public void setSearchLoaderVisible(boolean visible) {
        loader.setVisible(visible);
    }

    @Override
    public HasRemotePagingTableHandlers<CellPersonneModel> getRemotePagingTable() {
        return remotePagingTable;
    }

    @Override
    public void setButtonsColumnUpdater(FieldUpdater<CellPersonneModel, String> updater) {
        buttonsColumns.setFieldUpdater(updater);
    }

    @Override
    public SuggestListBoxSingle<IdLabelModel> getTypeSearchModelSuggestHandler() {
        return slbTypeModelSearch;
    }

}
